import errType from "../constant/err.type.js"
const { dataFormatError, verifyCodeError  } = errType

const validator = rules => {
  return async (ctx, next) => {
    try {
      ctx.verifyParams(rules)
    } catch (error) {
      dataFormatError.error = error.errors
      return ctx.app.emit('error', dataFormatError, ctx)
    }
    await next()
  }
}

// 验证验证码
const verifyCode = async (ctx, next) => {
  const { code } = ctx.request.body
  if (code !== ctx.session.code) return ctx.app.emit('error', verifyCodeError, ctx)
  await next()
}

export {
  validator,
  verifyCode
}